import request from '@/utils/request'

// 登录
export function login(data: { username: string; password: string }) {
  return request({
    url: '/api/auth/login',
    method: 'post',
    data
  }).then(res => {
    if (res.code === 200 && res.data.token) {
      // 保存 token 到 localStorage
      localStorage.setItem('token', res.data.token)
    }
    return res
  })
}

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/api/auth/info',
    method: 'get'
  })
}

// 获取用户权限
export function getUserPermissions() {
  return request({
    url: '/api/auth/permissions',
    method: 'get'
  })
}

// 退出登录
export function logout() {
  return request({
    url: '/api/auth/logout',
    method: 'post'
  }).then(() => {
    // 清除 token
    localStorage.removeItem('token')
  })
} 